package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.*;
import com.itheima.dto.EmployeeDTO;
import com.itheima.entity.Employee;
import com.itheima.mapper.EmployeeMapper;
import com.itheima.service.EmployeeService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.apache.commons.lang3.StringUtils;
import java.util.List;

/**
 * <p>
 * 员工信息 前端控制器
 * </p>
 *
 * @author fengzhu
 * @since 2024-10-10
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    //登录的，提供一个json根据restful规范进行写
    @PostMapping("/login")
    public R<Employee> Login(HttpServletRequest request, @RequestBody Employee employee) {
        //1.实现代码的md5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());//进行md5加密

        //2.查询数据库根据数据
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<Employee>();
         queryWrapper.eq(Employee::getUsername, employee.getUsername()); //查询条件
        Employee emp= employeeService.getOne(queryWrapper);
        //3.检查名字是不是空，名字是唯一的

        if(emp==null){
            return R.error("登录失败，重新输入用户名");
        }

        if(!emp.getPassword().equals(password)){
            return R.error("登录失败，密码错误");
        }
        //4.查看用户的的状态
        if(emp.getStatus()==0){
            return R.error("账号已经禁用");
        }

        //5.登录成功，将用户的id放到session里面
        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    /**
     * 员工退出
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {

            request.getSession().removeAttribute("employee");
             return R.success("成功退出");

    }

    /**
     * 新增员工
     * @param employee 反序化
     * @return
     */

    @PostMapping
    public R<String> addemployee(HttpServletRequest request, @RequestBody Employee employee) {

        Long empid  =(Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(empid);
        employee.setUpdateUser(empid);
        //设计代码
        String password = "123456";
        employee.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));

        employeeService.save(employee);
        return R.success("成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        log.info("page={}, pageSize={}, name={}", page, pageSize, name);

        // 构造分页对象
        Page<Employee> pageInfo = new Page<>(page, pageSize);

        // 构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        // 执行查询
        employeeService.page(pageInfo, queryWrapper);

        // 返回分页数据，封装成 R 对象
        return R.success(pageInfo);
    }

    /**
     * 根据我们的id来修改员工,启用禁用
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request ,@RequestBody Employee employee) {
     Long empId = (Long) request.getSession().getAttribute("employee");
     employee.setUpdateUser(empId);
     employeeService.updateById(employee);
     return R.success("修改成功");
    }

    /**
     * 实现了数据的回写
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> get(@PathVariable Long id) {
      Employee employee =  employeeService.getById(id);
      if(employee!=null){
          return R.success(employee);
      }
      return R.error("没有找到");
    }
}
